package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.pay.PayServiceImpl;
import com.ruoyi.project.pay.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class SMSUtil {
    private static final Logger log = LoggerFactory.getLogger(SMSUtil.class);
    public static JSONObject sendSms(String content, String phoneNum){
        String sendUrl = "http://api.quanqiusms.com/api/sms/mtsend";
        try{
            HashMap map = new HashMap();
            map.put("appkey","mvRlXG5X");//API账号
            map.put("secretkey","CXP0fVhoOQe3aa");//
            map.put("content",URLEncoder.encode(content,"UTF-8"));//短信内容
            map.put("phone",phoneNum);//手机号
            JSONObject js = (JSONObject) JSONObject.toJSON(map);
            log.info("发送短信内容："+js.toString());
            String sortKey =  Util.sortKey(map);
            String result = HttpUtils.sendPost(sendUrl,sortKey);
            log.info("短信发送结果："+ result);
            return JSONObject.parseObject(result);
        }catch (UnsupportedEncodingException error){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code","500");
            return jsonObject;
        }
    }
    private static String sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            OutputStream os=httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();
            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        JSONObject js = SMSUtil.sendSms("code: [9865] by ASA", "525544813588");
        System.out.println(js.toJSONString());
    }
}
